
package de.justphil.tcg.tcgandroidclient.network.websocket;

import java.net.URI;
import java.net.URISyntaxException;

import android.util.Log;

import de.justphil.tcg.tcgandroidclient.util.TCGLog;
import de.justphil.websocketclient.WebSocket;
import de.justphil.websocketclient.WebSocketConnection;
import de.justphil.websocketclient.WebSocketException;
import de.justphil.websocketclient.WebSocketListener;
import de.justphil.websocketclient.WebSocketMessage;

public class WebSocketTest {
	
	public WebSocketTest() {
	
		try {
			URI uri = new URI("ws://192.168.1.100:8888/game");
			TCGLog.d(new Object[] {
					"Connecting to WebSocket ", uri, "..." });
			WebSocket ws = new WebSocketConnection(uri);
			ws.setWebSocketListener(new WebSocketListener() {
				
				@Override
				public void onMessage(WebSocketMessage message) {
				
					TCGLog.d(new Object[] {
							"Received Message: ", message.getText() });
				}
				
				@Override
				public void onConnect() {
				
					TCGLog.d(new Object[] {
						"   Connected." });
				}
				
				@Override
				public void onClose() {
				
					TCGLog.d(new Object[] {
						"Disconnected." });
				}
			});
			ws.connect();
		}
		catch (WebSocketException e1) {
			TCGLog.e(new Object[] {
					e1.getClass().getSimpleName(), ": ", e1.getMessage() });
			Log.e("TCG", "Exception: ", e1);
		}
		catch (URISyntaxException e1) {
			TCGLog.e(new Object[] {
					e1.getClass().getSimpleName(), ": ", e1.getMessage() });
		}
	}
}
